<?php
class Tourismkeyword extends CS_Controller 
{
    private $status = [
        '0' => '禁用',
        '1' => '启用'
    ];
    public function _init() 
    {
        $this->load->library('pagination');
        $this->load->model('tourism_key_word_model','tourism_key_word');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $getData = $this->input->get();
        $getData['provider_id'] = $this->uid;
        $config['first_url'] = site_url('tourismkeyword/grid').$this->pageGetParam($this->input->get());
        $config['base_url'] = site_url('tourismkeyword/grid');
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['total_rows'] = $this->tourism_key_word->findByCount($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_key_word->findPageList($num,$page_num,$getData,'*','sort DESC');
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['status'] = $this->status;
        $this->load->view('tourismkeyword/grid', $data);
    }
    
    public function add()
    {
        $this->load->view('tourismkeyword/add');
    }
    
    public function addPost()
    {
        $param['keyword'] = trim($this->input->post('keyword',true));
        $param['sort'] = trim($this->input->post('sort',true));
        $param['provider_id'] = $this->uid;
        $param['status'] = $this->input->post('status',true);
        $param['create_time'] = date('Y-m-d H:i:s');
        $error = $this->validateAdd($param);
        if( !empty($error) ){
            $this->error('tourismkeyword/grid', '', $error);
        }
        $count = $this->tourism_key_word->findByCount(['keyword' => $param['keyword'], 'provider_id' => $param['provider_id']]);
        if ($count > 0) {
            $this->error('tourismkeyword/add', '', '该类别已经添加');
        }
        $status = $this->tourism_key_word->insert($param);
        if( $status ){
            $this->success('tourismkeyword/grid', '', '保存成功');
        } else{
            $this->error('tourismkeyword/add', '', '保存失败');
        }
    }
    
    public function edit($id)
    {
        $data['keyword'] = $this->tourism_key_word->findOne(['id' => $id])->row();
        $this->load->view('tourismkeyword/edit',$data);
    }
    
    public function editPost()
    {
        $param['keyword'] = trim($this->input->post('keyword',true));
        $param['sort'] = trim($this->input->post('sort',true));
        $param['status'] = $this->input->post('status',true);
        $id = $this->input->post('id');
        $error = $this->validateAdd($param);
        if( !empty($error) ){
            $this->error('tourismkeyword/grid', '', $error);
        }
        $result = $this->tourism_key_word->findOne(['keyword' => $param['keyword'], 'provider_id' => $param['provider_id']]);
        if ($result->num_rows() > 0 && ($result->row()->id == $id)) {
            $this->error('tourismkeyword/add', '', '该关键词已经添加');
        }
        $status = $this->tourism_key_word->update($param,['id' => $id]);
        if( $status ){
            $this->success('tourismkeyword/grid', '', '保存成功');
        } else{
            $this->error('tourismkeyword/add', '', '保存失败');
        }
    }
    
    /**
     * 删除
     * @param unknown $id
     */
    public function delete($id){
        
        $count = $this->tourism_key_word->findByCount(['id' => $id]);
        if($count <= 0){
            $this->error('tourismkeyword/grid', '', '该关键词不存在');
        }
        $status = $this->tourism_key_word->delete($id);
        if( $status ){
            $this->success('tourismkeyword/grid', '', '删除成功！');
        } else {
            $this->error('tourismkeyword/grid', '', '删除失败！');
        }
    }
    
    public function checkNameRepeat()
    {
        $keyword= trim( $this->input->get('keyword') );
        $id = trim( $this->input->get('id') );
        $result = $this->tourism_key_word->findOne(['provider_id' => $this->uid , 'keyword' => $keyword]);
        if( empty($id) ){  // 添加
            echo ( $result->num_rows() > 0 ) ? "false" : "true" ; exit();
        }else{ //修改
            echo ( $result->num_rows() > 0 && $result->row(0)->id != $id ) ? "false" : "true";exit();
        }
    }
    
    /**
     * 验证
     * @param unknown $param
     */
    public function validateAdd($param){
        
        $error = array();
        if( $this->validateParam($param['keyword']) ){
            $error['keyword'] = '关键词不能为空';
        }
        if( $this->validateParam($param['sort']) ){
            $error['sort'] = '排序不能为空';
        }
        if( !is_numeric($param['sort']) ){
            $error['sort_number'] = 'sort值必须为数字';
        }
        if (!in_array($param['status'], [0, 1])) {
            $error['status'] = '状态值错误';
        }
        return $error;
    }
}